package com.common.android.utils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.FragmentActivity;

/**
 * Activity栈管理相关工具类
 * <br>Created 2014-8-22 下午5:52:32
 * @version  
 * @author   huangyx		
 * @see 	 
 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 */
public class StackManager {

	private static List< WeakReference<FragmentActivity>> fragmentActivityList = 
	        new ArrayList<WeakReference<FragmentActivity>>();
	
	/**
	 * Activity加入堆栈
	 * <br>Created 2014-8-22 下午5:51:51
	 * @param activity 要加入的Activity
	 * @author       huangyx
	 */
	public static void putFragmentActivity(
			FragmentActivity activity) {
		if(!fragmentActivityList.contains(activity)){
			fragmentActivityList.add(new WeakReference<FragmentActivity>(activity) );	
		}
	}
	
	/**
	 * 关闭应用所有的Activity
	 * <br>Created 2014-8-22 下午5:51:10
	 * @author       huangyx
	 */
	public static void closeActivitys(){
		for(WeakReference<FragmentActivity> wrf : fragmentActivityList){
			if(wrf != null){
			    if (wrf.get() != null) {
			        wrf.get().finish();
                }
			}
		}
		fragmentActivityList.clear();
	}
	
	/**
	 * 弹出Activity栈的指定Activity
	 * <br>Created 2014-8-22 下午5:53:14
	 * @param activity 指定的Activity
	 * @author       huangyx
	 */
	public static void pullFragmentActivity(FragmentActivity activity){
		for(WeakReference<FragmentActivity> wrf : fragmentActivityList){
		    if(wrf != null){
		        if (wrf.get() != null) {
		            if(wrf.get().equals(activity)){
		                fragmentActivityList.remove(wrf);
		                wrf.clear();
		                return;
		            }
				}
			}
		}
	}
}
